package com.easybuy.controller;

import com.easybuy.Tools.Page;
import com.easybuy.pojo.Category;
import com.easybuy.pojo.Product;
import com.easybuy.service.CategoryService.CategoryService;
import com.easybuy.service.ProductService.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//关于分类的一切控制操作
public class Member_CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;

    @RequestMapping("/categoryList")
    @ResponseBody
    //分类管理（分页）
    public Page categoryList(Page page){
        return categoryService.allCategorys(page);
    }


    @RequestMapping("/getCategory")
    @ResponseBody
    //查询单个分类内容
    public Category geCategory(@RequestParam int id){
        return categoryService.selectById(id);
    }


    @RequestMapping("/addCategory")
    @ResponseBody
    //添加分类
    public Boolean addCategory(Category category){
        if(categoryService.addCategory(category)>0){
            return true;
        }else {
            return false;
        }
    }


    @RequestMapping("/modifyCategory")
    @ResponseBody
    //修改分类
    public boolean modifyCategory(Category category){
        if (categoryService.updateCategory(category)>0){
            return true;
        }
        else return false;
    }


    @RequestMapping("/deleCategory")
    @ResponseBody
    //删除分类
    public boolean deleCategory(int id){
        if (categoryService.deleCategory(id)>0){
            return true;
        }
        else return false;
    }


    @RequestMapping("/getParentCategory")
    @ResponseBody
    //通过分类级别查询其高一级的分类内容
    public List<Category> getParentCategory(@RequestParam int type){
        List<Category> list=categoryService.getCategoryBytype(type-1);
        return list;
    }


    @RequestMapping("/getCategoryByParentId")
    @ResponseBody
    //通过parentId查询分类
    public List<Category> getCategoryByParentId(@RequestParam int id){
        List<Category> list=categoryService.selectByParentId(id);
        return list;
    }


    @RequestMapping("/checkCategory")
    @ResponseBody
    //检查分类下是否有商品
    public boolean checkCategory(@RequestParam int id){
        Map map=new HashMap();
        Page page=new Page();
        page.setPageNow(1);
        page.setPageSize(5);
        map.put("id",id);
        map.put("page",page);
        List<Product> list=productService.selectProducts(map).getList();
        if (list.size()!=0){
            return true;
        }
        else {
            return false;
        }
    }


    @RequestMapping("/getCategoryByName")
    @ResponseBody
    //通过分类名字查询分类（检查分类名称是否重复）
    public Category getCategoryByName(@RequestParam String name){
        return categoryService.selectByName(name);
    }

}
